﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.BusiTravel;
using Enterprise.Service.BusiTravel;
using Enterprise.Service.User;
using Enterprise.Service.Department;
using Enterprise.Model.WorkFlow;
using Enterprise.Service.WorkFlow;

namespace Enterprise.UI.Web.Modules.BusiTravel
{
    public partial class Edit : Enterprise.Service.BasePage
    {
        string Id = (string)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnCommand();
                OnStart();
            }
        }

        private void OnCheck()
        {
            // panduan 
        }

        private void OnCommand()
        {
            if (Cmd == "Revo")
            {
                string _note = "操作成功";
                try
                {
                    BusiTravelService bService = new BusiTravelService();
                    var query = bService.BusiTravelDisp(Id);
                    if (query != null && query.USERID == Utility.Get_UserId)
                    {
                        query.DB_Option_Action = "Delete";
                        bService.BusiTravelExecute(query);
                        //同时删除审批流程中的审批节点
                        WorkFlowProcessService wpService = new WorkFlowProcessService();
                        wpService.WorkFlowProcessDelete(Id);
                    }
                }
                catch (Exception ex)
                {
                    _note = ex.Message;
                }
                Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Index.aspx");
            }
        }

        private void OnStart()
        {
            tb_StartTime.Text = DateTime.Now.ToShortDateString();
            tb_EndTime.Text = DateTime.Now.AddDays(30).ToShortDateString();
            lb_CreateTime.Text = DateTime.Now.ToString();
            int deptId = 0;
            tb_User.Text = UserService.GetUserName(Utility.Get_UserId, out deptId);
            tb_Dept.Text = DepartmentService.GetDepartMentName(deptId);
        }

        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                BusiTravelService oService = new BusiTravelService();
                BusiTravelEntity entity = new BusiTravelEntity();
                entity.DB_Option_Action = "Insert";
                entity.BID = Guid.NewGuid().ToString();
                entity.USERID = Utility.Get_UserId;
                entity.DEPTID = UserService.GetUserDeptId(entity.USERID);
                entity.BCREATETIME = DateTime.Now;
                entity.BSUBJECT = (string)Utility.sink(tb_Subject.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.BDESTINATION = (string)Utility.sink(tb_Distination.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.BLINE = (string)Utility.sink(tb_Line.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.BTICKETTYPE = (string)Utility.sink(tb_ThicketType.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.BSTARTIME = (DateTime)Utility.sink(tb_StartTime.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Date);
                entity.BENDTIME = (DateTime)Utility.sink(tb_EndTime.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Date);
                entity.BSTATE = 0;
                int wfTypeId=21;
                WorkFlowService wlService = new WorkFlowService();
                entity.BCHECKERS = wlService.GetWokrflowCheckUsers(wfTypeId, entity.USERID, entity.DEPTID);
                if (!string.IsNullOrEmpty(entity.BCHECKERS))
                {
                    oService.BusiTravelExecute(entity);
                    //发送待办消息至审批人处进行相关审批
                    oService.Notice(entity, 0, wfTypeId);
                }
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Index.aspx");
        }
    }
}